//app.js
App({
  onLaunch: function () {
    const { statusBarHeight, platform } = wx.getSystemInfoSync()
    const { top, height } = wx.getMenuButtonBoundingClientRect()

    // 状态栏高度
    wx.setStorageSync('statusBarHeight', statusBarHeight)
    // 胶囊按钮高度 一般是32 如果获取不到就使用32
    wx.setStorageSync('menuButtonHeight', height ? height : 32)
    
    // 判断胶囊按钮信息是否成功获取
    if (top && top !== 0 && height && height !== 0) {
        const navigationBarHeight = (top - statusBarHeight) * 2 + height
        // 导航栏高度
        wx.setStorageSync('navigationBarHeight', navigationBarHeight)
    } else {
        wx.setStorageSync(
          'navigationBarHeight',
          platform === 'android' ? 48 : 40
        )
    }

    //获取机型
    wx.getSystemInfo({
      success:  res=>{
        this.globalData.model = res.model
      }
    })
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        
        wx.request({
          url: this.globalData.url + '/user/login',
          data: {
            jsCode: res.code,
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: result => {
            var data = JSON.parse(result.data.data)

            console.log("app.js",data)
            this.globalData.userInfo.openid = data.openid
            this.globalData.userInfo.score = data.score
            this.globalData.newUser = data.newUser
            console.log("newUser:",data.newUser)
            this.globalData.checkReady = true
            if (this.userLoginReadyCallback) {
              this.userLoginReadyCallback("")
            }
            this.setUserInfo()
          }
        })
      }
    })
  
  },

  setUserInfo:function(option){
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          
          wx.getUserInfo({
            lang:"zh_CN",
            success: res => {
              console.log(res.userInfo)
              this.globalData.userInfo.nickName = res.userInfo.nickName
              this.globalData.userInfo.avatarUrl = res.userInfo.avatarUrl
              this.globalData.userInfo.country = res.userInfo.country
              this.globalData.userInfo.province = res.userInfo.province
              this.globalData.userInfo.city = res.userInfo.city
              this.globalData.userInfo.gender = res.userInfo.gender
              this.globalData.checkLogin = true
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback("")
              }
            }
          })
        }else{
          
          this.globalData.checkLogin = true
          if (this.userInfoReadyCallback) {
            this.userInfoReadyCallback("")
          }
        }
      }
    })
  },
  
  globalData: {
    url:"https://www.ydsungan.com",
    userInfo: {},
    answerArray:[],
    newUser:true,
    allBookList:[],
    questionList:[],
    checkLogin:false,
    selectedPost:null,
    allUsingBookList:[],
    errorSource:null,
    delerror:null,
    reviewList:null,
    searchFileList:null,
    selectedSearchFile:null,
    searchPostList:null,
    model:"",
    checkReady:false,
    setting:null,
    
  }
})